package com.example.freshfoodproject.controller;

import com.example.freshfoodproject.entry.Goods;
import com.example.freshfoodproject.entry.Shopping;
import com.example.freshfoodproject.service.ListService;
import com.example.freshfoodproject.util.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Controller
public class ListController {
    @Autowired
    private ListService listService;
    /*
    * 根据数据库中的信息动态生成下拉框
    * */
    @RequestMapping("/loadType")
    @ResponseBody
    public List<Goods> loadType(){
        return listService.findType();
    }

    /**
     *
     * @param type 生鲜的种类
     * @param goodsName 生鲜的名字
     * @param indexPage 当前页
     * @param showCount 每页数据的条数
     * @return 一个pageHelper集合，集合中有当前页的数据信息，当前页码，每一页的信息条数，总页数和数据总条数
     */
    @RequestMapping("/show")
    @ResponseBody
    public PageHelper show(String type,String goodsName, String indexPage,String showCount){

        Goods goods = new Goods();
        PageHelper pageHelper = new PageHelper();
        if (type!=""){
            goods.setType(type);
        }
        if (goodsName!=""){
            goods.setGoodsName(goodsName);
        }
        if (showCount!=null&&!showCount.isEmpty()) {
            pageHelper.setShowCount(Integer.parseInt(showCount));
        }
        if (indexPage!=null&&!indexPage.isEmpty()){
            pageHelper.setIndexPage((Integer.parseInt(indexPage)-1)*Integer.parseInt(showCount));
        }
        int dataCount = listService.findAll().size();
        pageHelper.setDataCount(dataCount);
        pageHelper.setPageCount(dataCount % Integer.parseInt(showCount) == 0 ?
                dataCount / Integer.parseInt(showCount) :
                dataCount / Integer.parseInt(showCount) + 1);
        ArrayList<Goods> list = (ArrayList<Goods>) listService.findGoodsByQueryAndIndex(goods, pageHelper);
        pageHelper.setList(list);

        return pageHelper;
    }

    /**
     *
     * @param type 生鲜的种类
     * @param goodsName 生鲜的名称
     * @param indexPage 当前页
     * @param showCount 每页数据的条数
     * @return 与上一个方法作用相同，唯一不同的pagehelper中的list集合按照价格排序
     */
    @RequestMapping("/sort")
    @ResponseBody
    public PageHelper sort(String type,String goodsName, String indexPage,String showCount){
        Goods goods = new Goods();
        PageHelper pageHelper = new PageHelper();
        if (type!=""){
            goods.setType(type);
        }
        if (goodsName!=""){
            goods.setGoodsName(goodsName);
        }
        if (showCount!=null&&!showCount.isEmpty()) {
            pageHelper.setShowCount(Integer.parseInt(showCount));
        }
        if (indexPage!=null&&!indexPage.isEmpty()){
            pageHelper.setIndexPage((Integer.parseInt(indexPage)-1)*Integer.parseInt(showCount));
        }
        int dataCount = listService.findAll().size();
        pageHelper.setDataCount(dataCount);
        pageHelper.setPageCount(dataCount % Integer.parseInt(showCount) == 0 ?
                dataCount / Integer.parseInt(showCount) :
                dataCount / Integer.parseInt(showCount) + 1);
        ArrayList<Goods> list = (ArrayList<Goods>) listService.findGoodsByQueryAndIndexSort(goods, pageHelper);
        pageHelper.setList(list);
        return pageHelper;
    }

    /**
     *
     * @param type 生鲜的类型
     * @param goodsName 生鲜的名字
     * @param indexPage 当前页
     * @param showCount 每页数据的条数
     * @return 与上一个方法作用相同，唯一不同的pagehelper中的list集合按照人气排序
     */
    @RequestMapping("/total")
    @ResponseBody
    public PageHelper total(String type,String goodsName, String indexPage,String showCount){
        Goods goods = new Goods();
        PageHelper pageHelper = new PageHelper();
        if (type!=""){
            goods.setType(type);
        }
        if (goodsName!=""){
            goods.setGoodsName(goodsName);
        }
        if (showCount!=null&&!showCount.isEmpty()) {
            pageHelper.setShowCount(Integer.parseInt(showCount));
        }
        if (indexPage!=null&&!indexPage.isEmpty()){
            pageHelper.setIndexPage((Integer.parseInt(indexPage)-1)*Integer.parseInt(showCount));
        }
        int dataCount = listService.findAll().size();
        pageHelper.setDataCount(dataCount);
        pageHelper.setPageCount(dataCount % Integer.parseInt(showCount) == 0 ?
                dataCount / Integer.parseInt(showCount) :
                dataCount / Integer.parseInt(showCount) + 1);
        ArrayList<Goods> list = (ArrayList<Goods>) listService.findGoodsByQueryAndIndexTotal(goods, pageHelper);
        pageHelper.setList(list);
        return pageHelper;
    }

    /**
     *
     * @param goodsName 生鲜的名称
     * @return 通过生鲜的名称获取该生鲜的全部信息
     */
    @RequestMapping("/selectOne")
    @ResponseBody
    public Goods selectOne(String goodsName){
        return listService.findName(goodsName);
    }

    /**
     * 该方法实现的是点击一个生鲜的链接后，后台会让它的人气数+1，在按照人气中排序时使用，同时，它还会对最近查看的数据进行修改,
     * 修改规则为:最近查看数据同时只会最多有五个数据，其他数据为0，当你点击一个生鲜的链接后，该生鲜的此数据变为1，
     * 其他数据如果值不等于0，那个每个数据+1，当数据大于5时，会重新为0，若你点击的生鲜链接本来该数据就不为0，
     * 那么数值比他大的生鲜不会进行+1的操作，保证最近查询一直有五条
     * @param goodsName 生鲜的名称
     */
    @RequestMapping("/updateTotal")
    @ResponseBody
    public void updateTotal(String goodsName){
        Goods goods = new Goods();
        goods.setGoodsName(goodsName);
        goods.setTotal(listService.findName(goodsName).getTotal()+1);
        if (listService.findName(goodsName).getRecentlyViewed()<=5){
            if (listService.findName(goodsName).getRecentlyViewed()>0){
                int count = listService.findName(goodsName).getRecentlyViewed();
                goods.setRecentlyViewed(1);
                for (Goods good : listService.findAll()){
                    if (good.getRecentlyViewed()>0&&good.getRecentlyViewed()<count&&!good.getGoodsName().equals(goodsName)){
                        good.setRecentlyViewed(good.getRecentlyViewed()+1);
                        listService.chageRecentlyViewed(good);
                    }
                }
            }else if (listService.findName(goodsName).getRecentlyViewed()==0){
                goods.setRecentlyViewed(1);
                for (Goods good : listService.findAll()) {
                    if (good.getRecentlyViewed()>0&&good.getRecentlyViewed()<5&&!good.getGoodsName().equals(goodsName)){
                        good.setRecentlyViewed(good.getRecentlyViewed()+1);
                        listService.chageRecentlyViewed(good);
                    }else if (good.getRecentlyViewed()==5){
                        good.setRecentlyViewed(0);
                        listService.chageRecentlyViewed(good);
                    }
                }
            }
        }
        listService.changeTotal(goods);
        listService.chageRecentlyViewed(goods);
    }

    /**
     *
     * @return 返回一个list集合，集合中的数据是最近点击的5个(如果从来没浏览过则是从1-5)
     */
    @RequestMapping("/showRecently")
    @ResponseBody
    public List<Goods> showRecently(){
        List<Goods> list = new ArrayList<>();
        for (Goods goods : listService.findAll()) {
            if (goods.getRecentlyViewed()!=0){
                list.add(goods);
            }
        }
        list.sort(Comparator.comparing(Goods::getRecentlyViewed));
        return list;
    }

    /**
     *
     * @return 返回的是商品表中的所有数据
     */
    @RequestMapping("/showGoodsAll")
    @ResponseBody
    public List<Goods> showGoodsAll(){
        return listService.findAll();
    }
}
